<?php include 'functions.php'; echo $html_doctype; ?>
<html>
<head>
<title> Scratch Resources | Register</title>
<?php
	//no metadata, no search engines on this page
	echo $html_meta_nosearch;
	echo $html_css;
	echo $html_scripts;
?>
<script type="text/javascript" src="/js/registerform.js"></script>
</head>

<body>
<!-- HEADER -->
<div id="maincontainer">
	<?php include 'header.php';?>
	<div class="box_fullwidth">
	<h1>Register</h1>
	
	<?php
	$error = false;
	$error_msg = '';

	if (isset($_POST['username'])) {
		$username = $_POST['username'];
		$password = $_POST['password'];
		$password2 = $_POST['password2'];
		$mail = $_POST['mail'];
		
		//recaptcha
		require_once('recaptchalib.php');
		$privatekey = "6Ldt4gkAAAAAAC_HJ5rcOplZzDfLrKXBlSARiB5q";
		$resp = recaptcha_check_answer ($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);		
		if (!$resp->is_valid) {
			$error = true;
			$error_msg .= 'You did not fill in the Text Identification correctly.<br>'."\n";
		}
		
		if (!$password==$password2) {
			$error = true;
			$error_msg .= 'The two passwords did not match.<br>'."\n";
		}
		if (!preg_match('/[A-Za-z0-9]+/',$password)) {
			$error = true;
			$error_msg .= 'The password did not consist of only characters and numbers.<br>'."\n";
		}
		if (!preg_match('/[A-Za-z0-9]+/',$username)) {
			$error = true;
			$error_msg .= 'The username did not consist of only characters and numbers.<br>'."\n";
		}
		if (strlen($password)<6 || strlen($password)>20) {
			$error = true;
			$error_msg .= 'The password should be between 6 and 20 characters long.<br>'."\n";
		}
		if (strlen($username)<3 || strlen($username)>20) {
			$error = true;
			$error_msg .= 'The username should be between 6 and 20 characters long.<br>'."\n";
		}
		$emailregex = '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i';
		if (!preg_match($emailregex,$mail)) {
			$error = true;
			$error_msg .= 'The E-mail address is not valid.<br>'."\n";
		}
	} else {
		$error = true;
	}
	
	if (!$error) {
		//handle request
		resdap_connect();
		// (autoId)       Username                Password                  Email      Rank        
		$qValues = "'" . $username . "', '" . md5($password) . "', '" . $mail . "', 'user');";
		$q =  "INSERT INTO res_users ( username, password, email, rank) VALUES(" . $qValues;
		//dev_out('Query: '.$q);
		mysql_query($q) or die(mysql_error());
		resdap_disconnect();
		echo 'Your Scratch account has been activated for use with Scratch Resources. <a href="/login">Login</a> to start using it!';
	} else {
		//show form
		//if ($error_msg) {
			echo '<div id="uploadform_errorbox">'.$error_msg.'</div><br>';
	 	//}
	?>
	<!-- REGISTER FORM -->
	<form class="defaultform" method="post" action="/register">
	<label for="username">Scratch Username:</label> <!-- (3 to 20 characters long) -->
	<input type="text" name="username" id="username"></input>
	<img src="/files/images/form_u.png" name="rescategory_icon" id="rescategory_icon"></img>
	<br>
	<br>
	<label for="username">Password:</label> <!-- (only letters and numbers allowed, 6-20 characters long) -->
	<input type="password" name="password" id="password"></input>
	<img src="/files/images/form_u.png" name="rescategory_icon" id="rescategory_icon"></img>
	<br>
	<br>
	<label for="username">Re-enter password:</label>
	<input type="password" name="password2" id="password2"></input>
	<img src="/files/images/form_u.png" name="rescategory_icon" id="rescategory_icon"></img>
	<br>
	<br>
	<label for="username">E-mail:</label> <!-- (3 to 20 characters long) -->
	<input type="text" name="mail" id="mail"></input><br>
	<br>
	
	<div id="recaptcha">
	<?php
	require_once('recaptchalib.php');
	$publickey = "6Ldt4gkAAAAAAE_TfQkjnCbCJikl_GT4QozaqYtk";
	echo recaptcha_get_html($publickey);
	?>
	</div>
	
	<br><br>
	
	<input type="submit" name="submit" id="submit" value="Singup"></input>
	</form>
	<!-- END FORM -->
	<?php 
	} //form shown
	echo '</div>'."\n";
	include('footer.php');
	?>
</div>
</body>

</html>
